package com.itheima.jieou;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Properties;

public class Factory {
    private static Properties prop= new Properties();
    public static HashMap<String,Object> map= new HashMap<>();
    static {
        try {
            prop.load(ClassLoader.getSystemResourceAsStream("bean.properties"));
            String daoClassName = prop.getProperty("IDao");
            IDao dao=null;
            try {
                Class<IDao> clazz = (Class<IDao>) Class.forName(daoClassName);
                dao = clazz.getDeclaredConstructor().newInstance();
            } catch (Exception e) {
                e.printStackTrace();
            }
            map.put("IDao",dao);

            String serviceClassName = prop.getProperty("IService");
            IService service=null;
            try {
                Class<IService> clazz = (Class<IService>) Class.forName(serviceClassName);
                service = clazz.getDeclaredConstructor().newInstance();
            } catch (Exception e) {
                e.printStackTrace();
            }
            map.put("IService",service);
            String controllerClassName = prop.getProperty("IController");
            IController controller=null;
            try {
                Class<IController> clazz = (Class<IController>) Class.forName(controllerClassName);
                controller = clazz.getDeclaredConstructor().newInstance();
            } catch (Exception e) {
                e.printStackTrace();
            }
            map.put("IController",controller);


        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static  IController getController() {


        return (IController) map.get("IController");
    }
    public static  IService getService(){

        return (IService) map.get("IService");
    }
    public static  IDao getDao(){


        return (IDao) map.get("IDao");
    }
}
